#!/usr/bin/env bash
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
# Function :CentOS7.X 系统操作
# Platform :RedHatEL7.x Based Platform
# Version  :1.2
# Date     :2021-07-06
# Author   :mugoLH
# Contact  :houxiaoshuai@baidu.com & hxsaj@126.com
# Company  :
# depend on:
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
set -e
#  函数列表 List of common functions
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----

#  色彩定义
#    指示 # echo -e "\n\033[1m 色彩测试-提示 \033[0m\n"
function show_tips() {
	echo -e "\033[1m $@ \033[0m" >&1;
}
#    提示 # echo -e "\n\033[1;38;46m 色彩测试-提示 \033[0m\n"
function show_notice() {
	echo -e "\033[1;38;46m $@ \033[0m" >&1;
}
#    警告 # echo -e "\n\033[1;35;46m 色彩测试-警告 \033[0m\n"
function show_warning(){
	echo -e "\033[1;35;46m $@ \033[0m" >&2;
}
#    报错 # echo -e "\n\033[1;31;46m 色彩测试-错误 \033[0m\n"
function show_error(){
	echo -e "\033[1;31;46m $@ \033[0m" >&2;
}

#  防火墙添加开放的端口
function firewalld_open_port() {
	# 检查firewalld是否运行
	if [ $(systemctl status firewalld| grep -w Active|awk -F "[(,)]" '{print$2}') == "running" ];then
	    # 检查是否目标端口已开
	    for i in $@ ;do
	    	if ! $(firewall-cmd --zone=public --list-ports | grep -qw ${i}); then
	    		firewall-cmd --zone=public --add-port=${i}/tcp --permanent >/dev/null 2>&1
	    		firewall-cmd --reload >/dev/null 2>&1
	    	fi
	    done
		# 展示所有已开端口
		# firewall-cmd --zone=public --list-ports
	else
		show_notice " firewalld 未运行 "
	fi
}
#  防火墙关闭并禁用
function firewalld_disable(){
	if [ $(systemctl list-unit-files|grep firewalld.service|awk '{print$2}') != "disabled" ];then
		systemctl disable firewalld >/dev/null 2>&1
	fi
	if [ $(firewall-cmd --state) == "running" ];then
		systemctl stop firewalld
	fi
}

#  关闭SELinux
function selinux_off() {
	if [ $(grep -w ^SELINUX /etc/selinux/config|awk -F "=" '{print$2}') == "enforcing" ];then
		sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
	fi
	if [[ $(getenforce) == "Enforcing" ]];then
		setenforce 0 >/dev/null 2>&1
	fi
}

#  安装软件
function yum_install() {
	yum install -y $@ >/dev/null 2>&1 || show_error "  软件安装失败 ！！！  "
}

